#include  "pwm_data.h"
#include "./freeRTOS/include/FreeRTOS.h"
#include "./freeRTOS/include/task.h"
#include "./freeRTOS/include/queue.h"


/* Library includes. */
#include "./stm32files/inc/stm32f10x_it.h"
#include "./stm32files/inc/stm32f10x_rtc.h"
#include "./stm32files/inc/stm32f10x_rcc.h"
#include "./stm32files/inc/misc.h"

 unsigned   TimerResolution = 16000 * 3;
unsigned  PrescalerResolution = 20;


PwmData PwmDataArray  [4];

unsigned
PwmNewSettings( unsigned _number, unsigned _range)
{
	unsigned  modifier =(TimerResolution / 20)/3;
	unsigned range = TimerResolution / 20 + 2* modifier;

	range  *= (unsigned)_number;
	range = range / (unsigned)_range;
	
	return range + TimerResolution / 20 - modifier;
}

void prvServiceUsbTask( void *pvParameters )
{
	int  i = 0;
for( ; i < 4; ++i )
{
(PwmDataArray+ i)->CurrentSetting = 0;
(PwmDataArray+ i)->Base = 10;
}
while(1)
{
// get  buffor  with  received  data
//set  new  pwm  settings
	setPWMEdges( 
	PwmNewSettings( PwmDataArray[ 0 ].CurrentSetting, PwmDataArray[ 0 ].Base),
	PwmNewSettings( PwmDataArray[ 1 ].CurrentSetting, PwmDataArray[ 1 ].Base),
	PwmNewSettings( PwmDataArray[ 2 ].CurrentSetting, PwmDataArray[ 2 ].Base),
	PwmNewSettings( PwmDataArray[ 3 ].CurrentSetting, PwmDataArray[ 3 ].Base)
	);
	
	vTaskSuspend( NULL );
}	
}
void OnUsbDataRx(uint8_t* dataIn, uint8_t length)
{
	if (length==4)
	{
		uint8_t index = *dataIn;
	PwmDataArray[ index ].CurrentSetting = *++dataIn;
	PwmDataArray[ index ].Base = *++dataIn;
}

}



